package com.maimao.user.controller.sys;

import com.maimao.api.sys.RoleApi;
import com.maimao.core.constant.AuthKey;
import com.maimao.core.constant.Constants;
import com.maimao.core.enums.RespCode;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.PageResult;
import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.DateUtils;
import com.maimao.user.controller.base.BaseController;
import com.maimao.user.service.sys.RoleService;
import com.maimao.model.user.sys.Role;
import com.maimao.model.user.sys.dto.DelDto;
import com.maimao.model.user.sys.dto.RoleSearchInput;
import com.maimao.model.user.sys.vo.RoleEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2020/12/6 4:34 下午
 */
@RestController
public class RoleController extends BaseController implements RoleApi {

    @Autowired
    private RoleService roleService;

    @PreAuthorize("hasRole('sysRoleCommon')")
    @Override
    public Result<?> search(Integer page, Integer size, RoleSearchInput input) {
        PageResult<RoleEx> search = roleService.search(page, size, input);
        return Result.okData(RespCode.OK, search);
    }

    @Override
    public Result<?> list() {
        return Result.okData(RespCode.OK, roleService.list());
    }

    @PreAuthorize("hasRole('sys-role')")
    @Override
    public Result<?> add(@Valid RoleEx roleEx, BindingResult bindingResult) {
        if (checkErrorAndInit(roleEx, bindingResult)) {
            return Result.notValid(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        if (!roleEx.getRoleCode().startsWith(AuthKey.DEFAULT_ROLE_PREFIX)){
            roleEx.setRoleCode(AuthKey.DEFAULT_ROLE_PREFIX+roleEx.getRoleCode());
        }
        roleService.addRole(roleEx);
        return Result.ok(RespCode.OK, "添加成功");
    }


    @PreAuthorize("hasRole('sys-role')")
    @Override
    public Result<?> update(@Valid RoleEx roleEx, BindingResult bindingResult) {
        if (checkErrorAndInit(roleEx, bindingResult)) {
            return Result.notValid(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        if (roleEx.getId() == null || roleEx.getId() < 1) {
            return Result.notValid("角色参数非法");
        }
        if (!roleEx.getRoleCode().startsWith(AuthKey.DEFAULT_ROLE_PREFIX)){
            roleEx.setRoleCode(AuthKey.DEFAULT_ROLE_PREFIX+roleEx.getRoleCode());
        }
        roleService.update(roleEx);
        return Result.ok(RespCode.OK, "修改成功");
    }

    @PreAuthorize("hasRole('sys-role')")
    @Override
    public Result<?> updateStatusBatch(DelDto delDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.notValid(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        if (!delDto.getStatus().equals(Constants.STATUS_1) && !delDto.getStatus().equals(Constants.STATUS_0)) {
            return Result.notValid("非法参数");
        }
        roleService.updateStatusBatch(delDto);
        return Result.ok(RespCode.OK, delDto.getStatus() == 1 ? "恢复成功" : "删除成功");
    }


    private boolean checkErrorAndInit(@Valid RoleEx roleEx, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return true;
        }
        if (roleEx.getStatus() == null) {
            roleEx.setStatus(1);
        }
        AuthUser authUser = checkAndGetAuthUser();
        roleEx.setCreateUser(Integer.valueOf(authUser.getId()));
        roleEx.setCreateTime(DateUtils.now());
        roleEx.setUpdateUser(Integer.valueOf(authUser.getId()));
        roleEx.setUpdateTime(DateUtils.now());
        return false;
    }

}
